package com.ossjk.api.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.Constant;
import com.ossjk.core.util.JwtTokenUtil;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.attendance.entity.Atdreport;
import com.ossjk.oa.attendance.service.IAtdreportService;
import com.ossjk.oa.attendance.vo.AtdreportVo;
import com.ossjk.oa.finance.service.IReimburseService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.*;
import com.ossjk.oa.system.vo.EmpDeptVo;
import com.ossjk.oa.system.vo.EmpVo;
import com.ossjk.oa.system.vo.EmployeeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 陈锐彬
 * 加班登记
 */

@RestController
@RequestMapping("/api/OvertimeReg")
//@Api(value = "OvertimeRegController加班登记控制器")
@Api(description = "加班登记接口")
public class WorkOvertimeController extends BaseController {

    @Autowired
    protected JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IAtdreportService iAtdreportService;
    @Autowired
    private IDepartmentService iDepartmentService;

    /**
     * 7.2 个人维护自己加班信息
     *
     */
    /**
     * 7.2.1 添加
     *
     * @param
     */

    @PostMapping("insert")
    @ApiOperation(value = "添加数据", notes = "添加数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "date", dataType = "date", required = true, value = "请假日期"),
            @ApiImplicitParam(paramType = "query", name = "duration", dataType = "BigDecimal", required = true, value = "请假时长"),
            @ApiImplicitParam(paramType = "query", name = "remarks", dataType = "BigDecimal", required = false, value = "请假原因")
    })
    public ResponseBean insert(Atdreport atdreport) {
        //以下为特殊情况时，字段所存储的数据
        //用户id
        if (CommonUtil.isBlank(atdreport.getEid())) {
            atdreport.setEid(getCurrnetUserId());
        }
        //部门id
        if (CommonUtil.isBlank(atdreport.getDid())) {
            atdreport.setDid(getDid());
        }
        String dateStr = CommonUtil.date0(atdreport.getDate());
        // 年
        atdreport.setYear(dateStr.substring(0, 4));
        // 月
        atdreport.setMonth(dateStr.substring(4, 6));
        // 日
        atdreport.setDay(dateStr.substring(6, 8));
        //类型：1-加班、2-带薪年假、3-带薪调休、4-普通请假，5-外出,非必填
        atdreport.setType(new BigDecimal(1));
        //审核状态：1-待审核、2-通过、3-不通过
        atdreport.setRwst(new BigDecimal(1));
        //请假时长
        if (CommonUtil.isBlank(atdreport.getDuration())) {
            return (ResponseBean) responseFail("请假时长不能为空");
        }
        if (iAtdreportService.insert(atdreport)) {
            return (ResponseBean) responseSuccess("新增成功");
        } else {
            return (ResponseBean) responseFail("新增失败");
        }

    }

    /**
     *
     * 7.2.2 修改[未审批的]
     * 去修改，回显数据(员工修改)
     */
    @GetMapping("toUpdate")
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @ApiImplicitParam(name = "id", paramType = "query", dataType = "String", required = true, value = "修改数据的id")
    public ResponseBean toUpdate(String id) {
        Atdreport atdreport = iAtdreportService.selectById(id);
        return (ResponseBean) responseSuccess(atdreport);
    }

    /**
     *
     * 修改(员工修改)
     */
    @PutMapping("update")
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "String", required = true, value = "请假数据id"),
            @ApiImplicitParam(paramType = "query", name = "mdtm", dataType = "date", required = true, value = "修改时间"),
            @ApiImplicitParam(paramType = "query", name = "remarks", dataType = "BigDecimal", required = false, value = "请假原因")
    })
    public ResponseBean update(Atdreport atdreport) {
        //以下为特殊情况时，字段所存储的数据
        //用户id
        if (CommonUtil.isEquals(atdreport.getRwst(), 2)) {
            return (ResponseBean) responseFail("审核已通过，不能再做修改");
        }
        if (CommonUtil.isBlank(atdreport.getId())) {
            return (ResponseBean) responseFail("id不能为空");
        }
        if (CommonUtil.isBlank(atdreport.getEid())) {
            atdreport.setEid(getCurrnetUserId());
        }
        //修改时间
        atdreport.setMdtm(new Date());
        //请假时长
        if (CommonUtil.isBlank(atdreport.getDuration())) {
            return (ResponseBean) responseFail("请假时长不能为空");
        }
        if (iAtdreportService.updateById(atdreport)) {
            return (ResponseBean) responseSuccess("修改成功");
        } else {
            return (ResponseBean) responseSuccess("修改失败");
        }


    }


    /**
     *
     * 7.2.3 删除[未审批的](员工删除)
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @ApiImplicitParam(name = "id", dataType = "String", paramType = "query", required = true, value = "数据id")
    @DeleteMapping("delete")
    public ResponseBean delete(String id) {
        if (CommonUtil.isBlank(id)) {
            return (ResponseBean) responseFail("id不能为空");
        }
        Wrapper wrapper = Condition.create().eq("type", 1).eq("id", id).eq("isdelete", 1).eq("rwst", 1);
        if (iAtdreportService.delete(wrapper)) {
            return (ResponseBean) responseSuccess("删除成功");
        } else {
            return (ResponseBean) responseFail("删除失败");
        }
    }

    /**
     * 部门列表
     */
    @GetMapping("DepartList")
    @ApiOperation(value = "部门列表", notes = "部门列表")
    public ResponseBean<List<Department>> DepartList() {
        //获取登录用户，查看是否有权限访问
        String eId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", eId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        Wrapper wrapper1 = Condition.create();
        if (empDeptVo.getLevel() == 2) {//2级别为总监，返回自己所属部门
            wrapper1.eq("name", empDeptVo.getDname());
        } else if (empDeptVo.getLevel() == 1) {//1级别为总经理，返回所有部门
        }
        List list = iDepartmentService.selectName(wrapper1);
        return (ResponseBean) responseSuccess(list);
    }

    /**
     * 员工列表
     */
    @GetMapping("empList")
    @ApiOperation(value = "员工列表", notes = "员工列表")
    @ApiImplicitParam(name = "did", paramType = "query", dataType = "String", value = "部门id")
    public ResponseBean<List<Employee>> empList(String did) {
        String eId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", eId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        List<EmpVo> empVos = null;
        if (empDeptVo.getLevel() == 2) {//返回自己所属部门下的员工
            empVos = iEmployeeService.selectEmpVosByDid(empDeptVo.getDid());
        } else if (empDeptVo.getLevel() == 1) {//根据部门来返回该部门下的员工
            empVos = iEmployeeService.selectEmpVosByDid(did);
        }
        return (ResponseBean<List<Employee>>) responseSuccess(empVos);
    }

    /**
     *
     * 7.1 个人可以查询自己的月度加班信息
     * 7.3.1 二级部门领导(总监一级)可以按部门查本部门的月度加班申请
     * level
     */
    @GetMapping("select")
    @ApiOperation(value = "查询月度加班信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "did", dataType = "string", value = "部门id"),
            @ApiImplicitParam(paramType = "query", name = "eid", dataType = "string", value = "用户id"),
            @ApiImplicitParam(paramType = "query", name = "month", dataType = "date", value = "月份")
    })

    public ResponseBean<List<Atdreport>> select(String did, String eid, String month) {
        //当eid为空时，默认查登录用户的信息
        if (CommonUtil.isBlank(eid)) {
            eid = getCurrnetUserId();
        }
        //当月份为空时，默认查本月
        if (CommonUtil.isBlank(month)) {
            Calendar now = Calendar.getInstance();
            month = CommonMethod.calMth(0, now.getTime()); // 默认本月
        }
        Wrapper wrapper = Condition.create().eq("id", eid);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        Wrapper wrapper1 = Condition.create().eq("type", 1);
        //当Level等级大于2时,只能查询自己的加班信息
        System.out.println(empDeptVo.getLevel());
        if (empDeptVo.getLevel() > 2) {
            wrapper1.eq("eid", eid).like("date", month);
        }
        //Level等级为2时，可查询自己部门下员工的加班信息
        else if (empDeptVo.getLevel() == 2) {
            //默认查询登录用户的部门
            wrapper1.eq("eid", eid).eq("did", empDeptVo.getDid()).like("date", month);
        }
        //当Level等级等于1时,能查询所有下属的加班信息
        else {
            wrapper1.eq("eid", eid).eq("did", did).like("date", month);
        }
        List<Atdreport> list = iAtdreportService.selectList(wrapper1);
        for (Atdreport atdreport : list) {
            System.out.println(atdreport);
        }
        return (ResponseBean<List<Atdreport>>) responseSuccess(list);
    }

    /**
     *
     * 7.3.2       对未处理的可以审批加班
     */
    @GetMapping("Approval")
    @ApiOperation(value = "审批加班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", required = true, dataType = "String", value = "数据id"),
            @ApiImplicitParam(name = "confirm", paramType = "query", required = true, dataType = "Integer", value = "判断执行哪个操作的标识")
    })
    public ResponseBean Approval(String id, Integer confirm) {
        //level等级为2以上才可以审批
        String UserId = getCurrnetUserId();
        Wrapper wrapper = Condition.create().eq("id", UserId);
        List<EmpDeptVo> empDeptVos = iEmployeeService.selectEmpDept(wrapper);
        EmpDeptVo empDeptVo = empDeptVos.get(0);
        System.out.println(empDeptVo);
        //通过id查询对应的用户数据
        Wrapper wrapper1 = Condition.create().eq("id", id).eq("rwst", 1);
        Atdreport atdreport = iAtdreportService.selectOne(wrapper1);
        System.out.println(empDeptVo.getLevel());
        if (empDeptVo.getLevel() <= 2) {
            if (confirm == 2) {
                atdreport.setRwst(new BigDecimal(2));
                atdreport.setRwid(empDeptVo.getId());//审核人id
                atdreport.setRwtm(new Date());//审核时间
                iAtdreportService.updateById(atdreport);
            } else if (confirm == 3) {
                atdreport.setRwst(new BigDecimal(3));
                atdreport.setRwid(empDeptVo.getId());//审核人id
                atdreport.setRwtm(new Date());//审核时间
                iAtdreportService.updateById(atdreport);
            }
            return (ResponseBean) responseSuccess("审批成功");
        } else {
            return (ResponseBean) responseFail("您暂无此权限");
        }
    }

    /**
     *
     * 7.4 按全公司人员的加班信息做月度导航 ([上一月][本月][下一月])
     */
    @ApiOperation(value = "加班月度导航", notes = "传入mth属性--例如：mth=2020-07")
    @ApiImplicitParam(name = "mth", value = "年月日期", paramType = "query", dataType = "String")
    @GetMapping("/OvertimeNav")
    public ResponseBean<List<Atdreport>> OvertimeNav(String mth) {

        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }

        return (ResponseBean<List<Atdreport>>) responseSuccess(iAtdreportService.selectPreNext(mth));
    }

}
